package com.tuanzi.loan.web.bean.family.push;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.push.LoanPushMessageSendTask;
import com.tuanzi.loan.business.service.push.LoanPushMessageSendTaskService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.push.LoanPushMessageSendTaskRequest;
import com.tuanzi.loan.vo.response.family.push.LoanPushMessageSendTaskResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.push.LoanPushMessageSendTaskLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.PUSH_MESSAGE_CHANNEL;
import static com.tuanzi.loan.business.constants.DictionaryCode.PUSH_MESSAGE_SEND_TASK_STATUS;

/**
 * @Author: chenweijun
 * @Date: 2018/12/12 14:26
 * @Description:推送消息发送任务Bean
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanPushMessageSendTaskBean extends JSFListingBean<LoanPushMessageSendTaskRequest, LoanPushMessageSendTaskResponse> {

    @Autowired
    private LoanPushMessageSendTaskService loanPushMessageSendTaskService;


    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new LoanPushMessageSendTaskRequest();
        lazyDataModel = new LoanPushMessageSendTaskLazyDataModel($);
    }

    /**
     * 创建发送任务
     * @return
     */
    public String onPushMessage(){
        return redirect("/views/family/user-info/push/send-push-message.xhtml", $);
    }


    public String onView(Long id){
        return redirect("/views/family/user-info/push/send-push-message.xhtml?id=" + id);
    }


    /**
     * 取消发送
     * @param id
     */
    public void onCancel(Long id){
        loanPushMessageSendTaskService.cancelTask(id);
        addSuccessfullyMessage();
    }

    /**
     * 重新发送
     * @param id
     */
    public void onResend(Long id){
        loanPushMessageSendTaskService.onResend(id);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(PUSH_MESSAGE_SEND_TASK_STATUS, PUSH_MESSAGE_CHANNEL);
    }

}
